﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
namespace DAO
{
    public class TinhDAO
    {
        //load danh sach Tinh
        public List<TinhDTO> LoadDanhSachTinh()
        {
            try
            {
                List<TinhDTO> list = new List<TinhDTO>();
                string sql = "select * from tinh";
                DataTable dt = Dataprovider.ExcuteQuery(sql);
                foreach (DataRow r in dt.Rows)
                {
                    TinhDTO t = new TinhDTO();
                    t.MaTinh = int.Parse(r[0].ToString());
                    t.TenTinh = r[1].ToString();
                    t.Phivanchuyen = int.Parse(r[2].ToString());
                    list.Add(t);
                }
                return list;
            }
            catch
            {
                return null;
            }
        }
        //Load danh sach tinh
        public static DataTable DSTinh()
        {
            DataService dservice = new DataService();
            dservice.Connect();
            DataTable dt = dservice.ExcuteQueryDataTable("Select * From Tinh");
            dservice.Disconnect();
            return dt;
        }
        //Lay phi van chuyen
        public static int PhiVanChuyen(int maTinh)
        {
            DataService dservice = new DataService();
            dservice.Connect();
            int dt = dservice.ExecuteNoneQuery("Select PhiVanChuyen From Tinh Where MaTinh="+maTinh);
            dservice.Disconnect();
            return dt;
        }
        //Kiem tra trung ten tinh
        public int KiemTraTinhTrung(string tentinh)
        {
            try
            {
                string sql = "select count(*) from tinh where tentinh = N'" + tentinh + "'";
                return int.Parse(Dataprovider.ExcuteScalar(sql));
            }
            catch
            {
                return 0;
            }
        }

        //Them tinh
        public int ThemTinh(TinhDTO tinhDTO)
        {
            try
            {
                string sql = "insert into Tinh values(N'" + tinhDTO.TenTinh + "','" + tinhDTO.Phivanchuyen + "')";
                return Dataprovider.ExcuteNonQuery(sql);
            }
            catch
            {
                return 0;
            }
        }
    }
}
